<template>
  <el-dialog :title="info.title" :visible.sync="visible" center width="460px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="100px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="24">
          <el-form-item label="物料" prop="lm_id" style="width: 100%">
            <el-select
              v-model="form.lm_id"
              size="small"
              :remote-method="selMats"
              remote
              filterable
              style="width: 100%"
              placeholder="请选择供应商"
              clearable
              @focus="selMats"
            >
              <el-option
                v-for="item in materials"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="供应商" prop="sup_id" style="width: 100%">
            <el-select
              v-model="form.sup_id"
              size="small"
              :remote-method="selSups"
              remote
              filterable
              style="width: 100%"
              placeholder="请选择供应商"
              clearable
              @focus="selSups"
            >
              <el-option
                v-for="item in suppliers"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="单价" prop="price" style="width: 100%">
            <el-input v-model="form.price" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="个数" prop="total" style="width: 100%">
            <el-input v-model="form.total" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark" style="width: 100%">
            <el-input v-model="form.remark" type="textarea" :rows="4" size="small" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleEdit()">保 存</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { querySupplier, selSups } from '@/api/supplier'
import { addMatInfo, updMatInfo, selMats, queryMaterial } from '@/api/material'
export default {
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      form: {},
      suppliers: [],
      materials: [],
      rates: [{ key: 'A', value: 'A' }, { key: 'B', value: 'B' }, { key: 'C', value: 'C' }, { key: 'D', value: 'D' }],
      rules: {
        lm_id: [{ required: true, trigger: 'blur', message: '物料不可为空' }],
        price: [{ required: true, trigger: 'blur', message: '单价不可为空' }],
        total: [{ required: true, trigger: 'blur', message: '个数不可为空' }],
        sup_id: [{ required: true, trigger: 'blur', message: '供应商不可为空' }]
      },
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.form = this.info.edit
        if (this.info.type === 'edit') {
          this.supplier()
          this.material()
        }
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    selMats(query) {
      let str = ''
      if (Object.prototype.toString.call(query) === '[object String]') str = query
      selMats({ name: str }).then(response => {
        this.materials = response.list
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    material() {
      this.$set(this, 'materials', [])
      queryMaterial({ id: this.form.lm_id }).then(response => {
        const info = {}
        info.key = response.info.id
        info.value = response.info.name
        this.materials.push(info)
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    selSups(query) {
      let str = ''
      if (Object.prototype.toString.call(query) === '[object String]') str = query
      selSups({ name: str }).then(response => {
        this.suppliers = response.list
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    supplier() {
      this.$set(this, 'suppliers', [])
      querySupplier({ id: this.form.sup_id }).then(response => {
        const info = {}
        info.key = response.info.id
        info.value = response.info.name
        this.suppliers.push(info)
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    handleEdit() {
      this.$refs['formDate'].validate(async(valid) => {
        if (valid) {
          if (this.info.type === 'add') {
            addMatInfo(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('编辑成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          } else {
            updMatInfo(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('编辑成功')
            }).catch(err => {
              this.$message.error(err.msg)
            })
          }
        }
      })
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style>

</style>
